#include "CalendarBlock.hpp"
#include "Clock.hpp"
#include "AssetManager.hpp"
#include "Notification.hpp"
#include "Util.hpp"
#include <cpp3ds/System/I18n.hpp>
#include <cpp3ds/System/FileSystem.hpp>
#include <TweenEngine/Tween.h>
#include <time.h>
#include <stdlib.h>

namespace Clock {

CalendarBlock::CalendarBlock()
: m_presetDay(None)
, m_isInverted(false)
, m_isSelected(false)
{
	m_calendarBlockDial.setOutlineColor(cpp3ds::Color(117, 117, 117));
	m_calendarBlockDial.setOutlineThickness(1);
	m_calendarBlockDial.setSize(cpp3ds::Vector2f(22.f, 22.f));
	m_calendarBlockDial.setOrigin(m_calendarBlockDial.getLocalBounds().width / 2, m_calendarBlockDial.getLocalBounds().height / 2);

	m_textDate.setCharacterSize(12);
	m_textDate.setFillColor(cpp3ds::Color::Black);
	m_textDate.useSystemFont();

	m_selectedSquare = m_calendarBlockDial;
	m_selectedSquare.setSize(cpp3ds::Vector2f(17.f, 17.f));
	m_selectedSquare.setOrigin(m_selectedSquare.getLocalBounds().width / 2, m_selectedSquare.getLocalBounds().height / 2);
	setSelectedColor(g_themeColor);
}

CalendarBlock::~CalendarBlock()
{

}

void CalendarBlock::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
{
	states.transform *= getTransform();

	target.draw(m_calendarBlockDial);

	if (m_isSelected)
		target.draw(m_selectedSquare);

	target.draw(m_textDate);
}

void CalendarBlock::update(float delta)
{
	m_tweenManager.update(delta);
}

void CalendarBlock::setBlockText(cpp3ds::String text)
{
	m_textDate.setString(text);
	m_textDate.setOrigin(m_textDate.getLocalBounds().width / 2, m_textDate.getLocalBounds().height / 2);
}

cpp3ds::String CalendarBlock::getBlockText()
{
	return m_textDate.getString();
}

void CalendarBlock::setPosition(float posX, float posY)
{
	m_calendarBlockDial.setPosition(posX + (m_calendarBlockDial.getLocalBounds().width / 2), posY + (m_calendarBlockDial.getLocalBounds().height / 2));
	m_textDate.setPosition(m_calendarBlockDial.getPosition().x - 1, m_calendarBlockDial.getPosition().y);
	m_selectedSquare.setPosition(m_calendarBlockDial.getPosition().x - 1, m_calendarBlockDial.getPosition().y - 1);
}

cpp3ds::Vector2f CalendarBlock::getPosition()
{
	return cpp3ds::Vector2f(m_calendarBlockDial.getPosition().x, m_calendarBlockDial.getPosition().y);
}

cpp3ds::Vector2f CalendarBlock::getSize()
{
	return m_calendarBlockDial.getSize();
}

void CalendarBlock::toggleInverted()
{
	m_isInverted = !m_isInverted;
	changePresets(m_presetDay, m_isInverted);
}

bool CalendarBlock::isInverted()
{
	return m_isInverted;
}

void CalendarBlock::setPreset(PresetDay newPreset)
{
	m_presetDay = newPreset;
	changePresets(m_presetDay, m_isInverted);
}

CalendarBlock::PresetDay CalendarBlock::getPreset()
{
	return m_presetDay;
}

void CalendarBlock::changePresets(PresetDay newPreset, bool isInverted)
{
	if (isInverted)
		m_textDate.setFillColor(cpp3ds::Color::White);
	else
		m_textDate.setFillColor(cpp3ds::Color::Black);

	if (newPreset == Saturday) {
		if (isInverted) {
			m_calendarBlockDial.setFillColor(cpp3ds::Color(63, 81, 181));
		} else {
			m_calendarBlockDial.setFillColor(cpp3ds::Color(63, 81, 181, 192));
		}
	} else if (newPreset == Sunday) {
		if (isInverted) {
			m_calendarBlockDial.setFillColor(cpp3ds::Color(244, 67, 54));
		} else {
			m_calendarBlockDial.setFillColor(cpp3ds::Color(244, 67, 54, 192));
		}
	} else {
		if (isInverted) {
			m_calendarBlockDial.setFillColor(cpp3ds::Color(158, 158, 158, 192));
		} else {
			m_calendarBlockDial.setFillColor(cpp3ds::Color::White);
		}
	}
}

void CalendarBlock::toggleSelected()
{
	m_isSelected = !m_isSelected;
}

bool CalendarBlock::isSelected()
{
	return m_isSelected;
}

void CalendarBlock::setSelectedColor(cpp3ds::Color newColor)
{
	m_selectedSquare.setOutlineColor(newColor);
	m_selectedSquare.setFillColor(cpp3ds::Color(newColor.r, newColor.g, newColor.b, 128));
}

cpp3ds::Color CalendarBlock::getSelectedColor()
{
	return m_selectedSquare.getOutlineColor();
}

} // namespace Clock
